package com.leetcode.offer;

/**
 * @author zfl
 * @Classname ReverseWords
 * @Description 翻转单词顺序
 * @Date 2020/11/26 17:41
 */
public class ReverseWords {

    /**
     * @param s
     * @return
     * 执行用时：2 ms, 在所有 Java 提交中击败了88.76%的用户
     * 内存消耗：38.3 MB, 在所有 Java 提交中击败了83.86%的用户
     */
    public static String reverseWords(String s) {
        String[] strings = s.trim().split(" ");

        StringBuilder stringBuilder = new StringBuilder();

        for (int i = strings.length - 1; i >= 0; i--) {
            if(strings[i].equals("")) {
                continue;
            }
            stringBuilder.append(strings[i] + " ");
        }

        return stringBuilder.toString().trim();
    }
}
